package com.ruoyi.system.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.YgabcBorrowMapper;
import com.ruoyi.system.domain.YgabcBorrow;
import com.ruoyi.system.service.IYgabcBorrowService;

/**
 * ygabc_borrowService业务层处理
 * 
 * @author ruoyi
 * @date 2022-06-15
 */
@Service
public class YgabcBorrowServiceImpl implements IYgabcBorrowService
{
    @Autowired
    private YgabcBorrowMapper ygabcBorrowMapper;

    /**
     * 查询ygabc_borrow
     * 
     * @param id ygabc_borrow主键
     * @return ygabc_borrow
     */
    @Override
    public YgabcBorrow selectYgabcBorrowById(String id)
    {
        return ygabcBorrowMapper.selectYgabcBorrowById(id);
    }

    @Override
    public List<YgabcBorrow> selectYgabcBorrowByuserId(String userId) {
        return ygabcBorrowMapper.selectYgabcBorrowByuserId(userId);
    }

    @Override
    public YgabcBorrow selectYgabcBorrowByName(String name) {
        return ygabcBorrowMapper.selectYgabcBorrowByName(name);
    }
    @Override
    public YgabcBorrow selectYgabcBorrowByuserName(String username) {
        return ygabcBorrowMapper.selectYgabcBorrowByuserName(username);
    }
    /**
     * 查询ygabc_borrow列表
     * 
     * @param ygabcBorrow ygabc_borrow
     * @return ygabc_borrow
     */
    @Override
    public List<YgabcBorrow> selectYgabcBorrowList(YgabcBorrow ygabcBorrow)
    {
        return ygabcBorrowMapper.selectYgabcBorrowList(ygabcBorrow);
    }

    /**
     * 新增ygabc_borrow
     * 
     * @param ygabcBorrow ygabc_borrow
     * @return 结果
     */
    @Override
    public int insertYgabcBorrow(YgabcBorrow ygabcBorrow)
    {
        return ygabcBorrowMapper.insertYgabcBorrow(ygabcBorrow);
    }

    /**
     * 修改ygabc_borrow
     * 
     * @param ygabcBorrow ygabc_borrow
     * @return 结果
     */
    @Override
    public int updateYgabcBorrow(YgabcBorrow ygabcBorrow)
    {
        return ygabcBorrowMapper.updateYgabcBorrow(ygabcBorrow);
    }

    /**
     * 批量删除ygabc_borrow
     * 
     * @param ids 需要删除的ygabc_borrow主键
     * @return 结果
     */
    @Override
    public int deleteYgabcBorrowByIds(String[] ids)
    {
        return ygabcBorrowMapper.deleteYgabcBorrowByIds(ids);
    }

    /**
     * 删除ygabc_borrow信息
     * 
     * @param id ygabc_borrow主键
     * @return 结果
     */
    @Override
    public int deleteYgabcBorrowById(String id)
    {
        return ygabcBorrowMapper.deleteYgabcBorrowById(id);
    }

    @Override
    public void updateYgabcBorrowStatus() throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        YgabcBorrow borrows = new YgabcBorrow();
        List<YgabcBorrow> borrowList = ygabcBorrowMapper.selectYgabcBorrowList(borrows);
        for (YgabcBorrow ygabcBorrow : borrowList) {
            if (ygabcBorrow.getReturnTime() != null || ygabcBorrow.getStatus() == 2)
                continue;
            String borrowTime = sdf.format(ygabcBorrow.getBorrowTime());
            Date borrow = sdf.parse(borrowTime);
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(borrow);
            calendar.add(Calendar.DATE, 15);
            Date isReturnTime = calendar.getTime();
            Date newTime = new Date();
            if (isReturnTime.before(newTime)) {
                System.out.println("成功执行定时任务");
                ygabcBorrow.setStatus(Long.valueOf("2"));
                int i = ygabcBorrowMapper.updateYgabcBorrow(ygabcBorrow);
            }
        }

    }
}
